package com.zprmb.udzt;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.ToastUtil;
import com.zprmb.udzt.util.VolleryUtil;

public class Reset_pwd extends BaseActivity implements OnClickListener {
	private EditText et_jy_pwd, et_confirm;
	private Button btn_confirm;
	private ImageView img_back, pass, pass2;
	private RelativeLayout rl_back;
	private TextView tv_customer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reset_pwd);
		BbtApplication.getInstance().addActvity(this);
		et_jy_pwd = (EditText) findViewById(R.id.et_jy_pwd);
		et_confirm = (EditText) findViewById(R.id.et_confirm);
		btn_confirm = (Button) findViewById(R.id.btn_confirm);
		img_back = (ImageView) findViewById(R.id.img_back);
		pass = (ImageView) findViewById(R.id.img_pass);
		pass2 = (ImageView) findViewById(R.id.img_pass2);
		rl_back = (RelativeLayout) findViewById(R.id.rl_back);
		tv_customer = (TextView) findViewById(R.id.enterprise_tel);
		tv_customer.setOnClickListener(this);
		et_jy_pwd.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (et_jy_pwd.getText().toString().equals("")) {
					pass.setVisibility(View.GONE);
				} else {
					pass.setVisibility(View.VISIBLE);
				}
			}
		});
		et_confirm.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (et_confirm.getText().toString().equals("")) {
					pass2.setVisibility(View.GONE);
				} else {
					pass2.setVisibility(View.VISIBLE);
				}
			}
		});
		et_confirm
		.setTransformationMethod(PasswordTransformationMethod
				.getInstance());
		et_jy_pwd.setTransformationMethod(PasswordTransformationMethod
				.getInstance());
		et_jy_pwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});  
		et_confirm.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});  
		rl_back.setOnClickListener(this);
		pass.setOnClickListener(this);
		pass2.setOnClickListener(this);
		img_back.setOnClickListener(this);
		btn_confirm.setOnClickListener(this);

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}

	private void initData() {
		// TODO Auto-generated method stub
		AjaxParams params = new AjaxParams();
		params.put("newPassword", et_jy_pwd.getText().toString());
		params.put("type", "resetPayPass");
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("输出", jsonObject.toString());
				if (jsonObject.optInt("success") == 1) {
					Intent intent = new Intent(Reset_pwd.this,
							Updata_Success.class);
					intent.putExtra("title", "修改交易密码");
					intent.putExtra("status", "修改成功");
					startActivity(intent);
					Reset_pwd.this.finish();
				}else{
					ToastUtil.showCenterToast(Reset_pwd.this, jsonObject.optString("comment"));
				}
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(1);
		volleryUtil.setShow(true);
		volleryUtil.execute(Reset_pwd.this, Constant.URL
				+ "/api/v5/pass/step2?", params.toString());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_confirm:
			if (et_jy_pwd.getText().toString().equals("")) {
				Toast.makeText(Reset_pwd.this, "交易密码不能为空", 2000).show();
				return;
			}
			if (et_jy_pwd.getText().toString().length() < 6) {
				Toast.makeText(Reset_pwd.this, "交易密码必须为六位纯数字", 2000).show();
				return;
			}
			if (et_jy_pwd.getText().toString()
					.equals(et_confirm.getText().toString())) {
				initData();

			} else {
				Toast.makeText(Reset_pwd.this, "两次输入密码不一致", 2000).show();
			}

			break;

		case R.id.img_back:
			finish();
			break;
		case R.id.img_pass:
			if (et_jy_pwd.getTransformationMethod().equals(
					PasswordTransformationMethod.getInstance())) {
				et_jy_pwd
						.setTransformationMethod(HideReturnsTransformationMethod
								.getInstance());
				pass.setBackgroundResource(R.drawable.yan1);
			} else {
				et_jy_pwd.setTransformationMethod(PasswordTransformationMethod
						.getInstance());
				pass.setBackgroundResource(R.drawable.yan2);
			}
			break;
		case R.id.img_pass2:
			if (et_confirm.getTransformationMethod().equals(
					PasswordTransformationMethod.getInstance())) {
				et_confirm
						.setTransformationMethod(HideReturnsTransformationMethod
								.getInstance());
				pass2.setBackgroundResource(R.drawable.yan1);
			} else {
				et_confirm.setTransformationMethod(PasswordTransformationMethod
						.getInstance());
				pass2.setBackgroundResource(R.drawable.yan2);
			}
			break;

		case R.id.rl_back:
			finish();
			break;
		case R.id.enterprise_tel:
			BaseHelper.showDialog2(this, "拨号", "400-0101-016", 1);
			break;
		}

	}

}
